namespace Guia.Elements
{
    using System.Windows.Automation;
    using Guia.Locator;

    /// <summary>
    /// This class represents a TreeView. A TreeView can have multiple <see cref="TreeViewItem"/>.
    /// </summary>
    public class TreeView : AbstractTreeView
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TreeView"/> class.
        /// </summary>
        /// <param name="automationElement">
        /// The automation element.
        /// </param>
        public TreeView(AutomationElement automationElement)
            : base(automationElement)
        {
        }

        /// <summary>
        /// Gets the default-condition to find a control by its type.
        /// </summary>
        public static SearchCondition ControlTypeCondition
        {
            get { return SearchCondition.ByControlType(ControlType.Tree); }
        }

        /// <summary>
        /// Gets the selected TreeViewItem.
        /// </summary>
        public TreeViewItem SelectedItem
        {
            get
            {
                AutomationElement[] selectedElements = SelectionPattern.Current.GetSelection();
                if (selectedElements.Length == 1)
                {
                    return new TreeViewItem(selectedElements[0]);
                }

                return null;
            }
        }

        /// <summary>
        /// Gets the SelectionPattern.
        /// </summary>
        private SelectionPattern SelectionPattern
        {
            get
            {
                return this.GetPattern<SelectionPattern>();
            }
        }
    }
}